from product_upload.util.basic.mysql_util import db_get_one


class AmazonUsTag:
    # 字段顺序与数据库字段一致
    fields = ['id', 'basic_id', 'basic_platform', 'basic_sku', 'base_json', 'extra_json', 'class_name', 'product_type',
              'create_time', 'update_time', 'remark', 'update_by']

    def __init__(self, **kwargs):
        self.id = kwargs.get('id', None)
        self.basic_id = kwargs.get('basic_id', None)
        self.basic_platform = kwargs.get('basic_platform', None)
        self.basic_sku = kwargs.get('basic_sku', None)
        self.base_json = kwargs.get('base_json', None)
        self.extra_json = kwargs.get('extra_json', None)
        self.class_name = kwargs.get('class_name', None)
        self.product_type = kwargs.get('product_type', None)
        self.create_time = kwargs.get('create_time', None)
        self.update_time = kwargs.get('update_time', None)
        self.remark = kwargs.get('remark', None)
        self.update_by = kwargs.get('update_by', None)

    @classmethod
    def from_db(cls, field_names, row) -> 'AmazonUsTag':
        if len(field_names) != len(row):
            raise ValueError("bug!---db field len != query field len")
        data_dict = dict(zip(field_names, row))
        return cls(**data_dict)

    def __repr__(self):
        field_info = []
        for field in self.fields:
            value = getattr(self, field)
            field_info.append(f"{field}={repr(value)}")
        return f"AmazonUsTag=[{', '.join(field_info)}]"


if __name__ == '__main__':
    print(db_get_one("amazon_us_tag", "id=3", AmazonUsTag))
    pass
